﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Configuration;
using Model;
using DAL;

namespace BLL
{
    /// <summary>
    ///法院数据服务中心
    /// </summary>
    public class FyDataService
    {
        /// <summary>
        /// 根据法院代码获取该法院下的子法院。
        /// </summary>
        public static Obj_B_FY[] GetFyChildrenFyObject(string ffydm)
        {
             string fbdm = GetFBDM(ffydm);
            Obj_B_FY[] fys = new Obj_B_FY[0];
            if (fbdm.Length >= 3 )
            {
                string likeStr = "";
                if (fbdm == "000") { likeStr="%00"; }
                if (fbdm[0] != '0' && fbdm[1] == '0' && fbdm[2] == '0') { likeStr = string.Format("{0}%0", fbdm[0]); }
                if (fbdm[0] != '0' && fbdm[1] != '0' && fbdm[2] == '0') { likeStr = string.Format("{0}{1}%", fbdm[0], fbdm[1]); }
                

                string conStr = DBConnector.GetConnectString(ffydm);
                string selectStr = string.Format("select * from {0} where fbdm like '{1}' and fbdm<>'{2}'", Obj_B_FY.TableName , likeStr, fbdm);
                DataTable dt = DBConnector.GetSqlSelectData(selectStr, conStr);
                if (dt != null && dt.Rows.Count > 0)
                {
                    int count = dt.Rows.Count;
                    fys = new Obj_B_FY[count];
                   
                    for (int i = 0; i < count; i++)
                    {
                        fys[i] = new Obj_B_FY(dt.Rows[i]);
                    }
                }
            }
            return fys;
        }

        /// <summary>
        /// 根据法院代码获取自身和该法院下的子法院对象。
        /// </summary>
        public static Obj_B_FY[] GetFySelfAndChildrenFyObject(string ffydm)
        {
            Obj_B_FY[] a = GetFyChildrenFyObject(ffydm);
            int leng = a.Length;

            Obj_B_FY[] b = new Obj_B_FY[leng+1];
            b[0] = GetFyObject(ffydm);
            for (int i = 0; i<leng;i++ )
            {
                b[i + 1] = a[i];
            }
            return b;

        }

        /// <summary>
        /// 根据法院代码获取法标代码
        /// </summary>
        public static string GetFBDM(string fydm)
        {
           
            string conStr = DBConnector.GetConnectString(fydm);
            string selectStr = string.Format("select fbdm from {0} where dm={1}", Obj_B_FY.TableName, fydm);
            DataTable dt = DBConnector.GetSqlSelectData(selectStr, conStr);
            string result = "";
            if (dt != null && dt.Rows.Count > 0)
            { result = dt.Rows[0]["fbdm"].ToString(); }
            return result;
        }

        /// <summary>
        /// 根据法院代码获取法院对象
        /// </summary>
        public static Obj_B_FY GetFyObject(string fydm)
        {
            Obj_B_FY fy = null;
            string conStr = DBConnector.GetConnectString(fydm);
            string selectStr = string.Format("select * from {0} where dm={1}", Obj_B_FY.TableName, fydm);
            DataTable dt = DBConnector.GetSqlSelectData(selectStr, conStr);
            if (dt != null && dt.Rows.Count > 0)
            {
                fy = new Obj_B_FY(dt.Rows[0]);
            }
            return fy;
        }

        /// <summary>
        /// 根据法院代码在配置表中查找相应的数据库名。
        /// </summary>
        public static string GetDBNameByFYDM(string fydm)
        {
            string ss = ConfigurationManager.AppSettings["FYDM_" + fydm];
           
            return ss;
        }

        /// <summary>
        /// 获取联网数据库名
        /// </summary>
        public static string GetLWDBName()
        {
            string ss = ConfigurationManager.AppSettings["LWDBname"];

            return ss;
        }


        /// <summary>
        /// 使用该网站系统的法院代码
        /// </summary>
        /// <returns></returns>
        public static string GetDefaultFYDM()
        {
            string ss = ConfigurationManager.AppSettings["DefaultFYDM"];

            return ss;
        }

        /// <summary>
        /// 获取联网协查协控数据库名
        /// </summary>
        public static string GetLWXCXKDBName()
        {
            string ss = ConfigurationManager.AppSettings["LWXCXKDBname"];

            return ss;
        }
    }
}
